// pages/home/home.js
import {
  showSuccess,
  showText
} from "../../utils/util"
import {
  takephonenumber
} from '@/api/register'
import {
  sendphonecreatecode,
  shopmalogin
} from '@/api/shop'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUri: getApp().globalData.imgUri,
    acsel1: false,
    acsel2: false,
    phoneState: true,
    phone: '',
    codeState: true,
    code: '',
    downCount: 60,
    codeText: '获取验证码',
    randStr:'',
    shopPropAgreement:false,
    showPropPrivacy:false

  },
  cli_privacy(){
    this.setData({
      showPropPrivacy:true
    })
  },
  closePropPrivacy(){
    this.setData({
      showPropPrivacy:false
    })
  },
  cli_agreement(){
    this.setData({
      shopPropAgreement:true
    })
  },
  closePropAgreement(){
    this.setData({
      shopPropAgreement:false
    })
  },
  // 获取手机号验证码
  getPhoneCode(){
    if (!this.data.phone) {
      this.setData({
        phoneState: false
      })
      return
    }
    sendphonecreatecode({
      phone: this.data.phone
    }).then(res => {
      if (res.code == 200) {
        this.setData({
          codeState: true,
          randStr:res.data.randStr
        })
        this.downCount()
      }
    })
  },
  // 执行倒计时
  downCount() {
    let t = this;
    let downCount = t.data.downCount;
    if (downCount == 0) {
      t.setData({
        downCount: 60,
        codeText: '获取验证码',
        codeState: true
      })
      return
    }
    downCount--;
    t.setData({
      downCount: downCount,
      codeText: downCount + 's'
    })
    setTimeout(() => {
      t.downCount()
    }, 1000)
  },
  selone() {
    this.setData({
      acsel1: !this.data.acsel1
    })
  },
  selone2() {
    this.setData({
      acsel2: !this.data.acsel2
    })
  },
  phoneInput(e) {
    this.setData({
      phone: e.detail.value,
      phoneState: true
    })
  },
  codeInput(e) {
    this.setData({
      code: e.detail.value,
      codeState: true
    })
  },
  register() {
    if (!this.data.phone) {
      this.setData({
        phoneState: false
      })
      return
    }
    if (!this.data.code) {
      this.setData({
        codeState: false
      })
      return
    }
    if (!this.data.acsel1) {
      showText('请阅读并同意隐私政策')
      return
    }
    if (!this.data.acsel1) {
      showText('请阅读并同意商业信息')
      return
    }
    let o = {
      checkPhoneNum: this.data.phone,
      phoneCode: this.data.code,
      randStr: this.data.randStr
    }
    shopmalogin(o).then(res => {
      if (res.code == 200) {
        var shopToken = res.data.shopToken;
        wx.setStorageSync('shopToken', shopToken);
        wx.redirectTo({
          url: '/shop/writeoff/writeoff',
        })
      }else{
        showText(res.msg)
      }
    })
  },
  back() {
    wx.navigateBack()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})